﻿using CefSharp;
using CefSharp.WinForms;
using System.Windows.Forms;

namespace CefDemo
{
    public partial class MainForm : Form
    {
        private ChromiumWebBrowser browser;

        public MainForm()
        {
            InitializeComponent();
            InitializeBrowser();
        }

        private void InitializeBrowser()
        {
            // 创建浏览器
            Cef.Initialize(new CefSettings
            {
                CefCommandLineArgs =
                {
                    { "disable-web-security", "1" },    // 禁用同源策略(仅开发环境使用)
                    //{ "allow-file-access-from-files", "1" },
                    //{ "allow-universal-access-from-files", "1" }
                }
            });
            browser = new ChromiumWebBrowser(string.Format(@"{0}\Web\index.html", Application.StartupPath))
            {
                Dock = DockStyle.Fill,
            };

            // 注册js对象容器
            browser.JavascriptObjectRepository.Register("methodInvoker", new MethodInvoker(), true, BindingOptions.DefaultBinder);
            this.Controls.Add(browser);
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            Cef.Shutdown();
            base.OnFormClosing(e);
        }
    }
}
